package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.model.vo.AddressVo;
import cn.edu.xmu.oomall.customer.service.AddressService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.edu.xmu.oomall.core.util.Common.processFieldErrors;

/**
 * @author Jiawei Zheng
 * @date 2021-12-09
 */
@RestController
@Slf4j
@RefreshScope
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class AddressController {

    @Autowired
    AddressService addressService;

    @Autowired
    private HttpServletResponse httpServletResponse;

    @ApiOperation(value = "买家新增地址")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "body",dataType = "Object",name = "body",value = "可填写的信息",required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @PostMapping(value = "/addresses")
    public Object addAddress(@LoginUser Long userId, @LoginName String userName, @Valid @RequestBody AddressVo addressVo,
                           BindingResult bindingResult){
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return returnObject;
        }
        ReturnObject ret= addressService.addAddress(userId, userName, addressVo);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "买家查询所有已有的地址信息")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(name = "page",dataType = "Integer",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",dataType = "Integer",value = "每页数目",required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @GetMapping(value = "/addresses")
    public Object getAllAddresses(
            @LoginUser Long userId,
            @RequestParam(name = "page", required = false) Integer page,
            @RequestParam(name = "pageSize",  required = false) Integer pageSize){
        ReturnObject ret = addressService.getAllAddresses(userId, page, pageSize);
        return Common.decorateReturnObject(ret);
    }

    @ApiOperation(value = "买家设置默认地址")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "path", name = "id",dataType = "Integer",value = "地址id",required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @PutMapping(value = "/addresses/{id}/default")
    public Object setDefaultAddress(
            @LoginUser Long userId,
            @LoginName String userName,
            @PathVariable("id")Long id){
        ReturnObject ret = addressService.setDefaultAddress(userId, userName, id);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 买家修改自己的地址信息
     * @param userId
     * @param userName
     * @param id
     * @param addressVo
     * @return
     */
    @ApiOperation(value = "买家修改自己的地址信息")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "path", name = "id",dataType = "Integer",value = "地址id",required = true),
            @ApiImplicitParam(paramType = "body",dataType = "Object",name = "body",value = "可修改的地址信息",required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @PutMapping(value = "/addresses/{id}")
    public Object updateAddress(
            @LoginUser Long userId,
            @LoginName String userName,
            @PathVariable("id")Long id,
            @Valid @RequestBody AddressVo addressVo){
        ReturnObject ret = addressService.updateAddress(userId, userName, id, addressVo);
        return Common.decorateReturnObject(ret);
    }

    /**
     * 买家删除地址
     * @param id
     * @return
     */
    @ApiOperation(value = "买家删除地址")
    @ApiImplicitParams(value={
            @ApiImplicitParam(paramType = "header", dataType = "String", name = "authorization", value = "用户token", required = true),
            @ApiImplicitParam(paramType = "path", name = "id",dataType = "Integer",value = "地址id",required = true),
    })
    @ApiResponses(value = {
            @ApiResponse(code = 0, message = "成功")
    })
    @Audit
    @DeleteMapping(value = "/addresses/{id}")
    public Object deleteAddress(
            @LoginUser Long userId,
            @PathVariable("id")Long id){
        ReturnObject ret = addressService.deleteAddress(userId, id);
        return Common.decorateReturnObject(ret);
    }
}
